import { createRouter, createWebHistory } from 'vue-router'
import Layout from "@/layout/index.vue"
import myCookies from '@/utils/myCookies'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: 'layout',
      component: Layout,
      children: [
        {
          path: '/',
          redirect: '/home'
        },
        {
          path: '/home',
          name: 'home',
          component: () => import("@/views/home/index.vue"),
        },
        {
          path: '/Search',
          name: 'Search',
          component: () => import("@/views/searchProduct/index.vue"),
        },
        //采花车路由
        {
          path: '/cart',
          name: 'cart',
          component: () => import("@/views/cart/index.vue")
        },

        // 商品详情路由
        {
          path: '/product/detail/:id',
          name: 'ProductDetail',
          component: () => import("@/views/productDetail/index.vue")
        },

        // 订单
        {
          path: '/order',
          name: 'order',
          component: () => import("@/views/order/index.vue")
        }
        //客服
        , {
          path: '/cs',
          name: 'cs',
          component: () => import("@/views/cs/index.vue")
        }
        , // 评价界面
        {
          path: '/appraise',
          name: 'appraise',
          component: () => import("@/views/appraise/index.vue")
        }
        , {
          path: '/center',
          name: 'center',
          component: () => import("@/layout/center/index.vue"),
          children: [
            // 个人信息
            {
              path: '/user/info',
              name: 'userInfo',
              component: () => import("@/views/user/index.vue")
            },
            // 收货地址
            {
              path: '/address',
              name: 'address',
              component: () => import("@/views/user/address/index.vue")
            },
            // 订单路由
            {
              path: '/orders',
              name: 'orders',
              component: () => import("@/views/orders/index.vue")
            },
            // 评价晒单
            {
              path: '/myComments',
              name: 'myComments',
              component: () => import("@/views/orders/myComments/index.vue")
            },
            // 关注的商品
            {
              path: '/follow',
              name: 'follow',
              component: () => import("@/views/follow/index.vue")
            },
            // 关注的商家
            {
              path: '/followVenderList',
              name: 'followVenderList',
              component: () => import("@/views/follow/followVenderList/index.vue")
            },


          ]
        }

        //错误路由重定向
        ,
        {
          path: '/:pathMatch(.*)*',
          redirect: '/home'
        }
      ]
    }

    , {
      path: '/login',
      name: 'login',
      component: () => import("@/views/LoginView.vue")
    }, {
      path: '/register',
      name: 'register',
      component: () => import('@/views/RegisterView.vue')
    }
    //错误路由重定向
    ,
    {
      path: '/:pathMatch(.*)*',
      redirect: '/home'
    }
  ],
})
// 全局前置钩子
router.beforeEach((to, from, next) => {
  // 如果用户未登录，跳转到登录页
  if (to.name === "cs" && myCookies.getToken() === undefined) {
    next({
      path: '/login',
      query: { redirect: to.fullPath } // 传递重定向参数
    })
  } else {
    next()
  }
})
export default router
